package com.ftg.learn.chapter03.util;

import lombok.Getter;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author kangjx
 * @date 2022/11/3
 */
@Component
public class SqlSessionUtil {

    @Resource
    @Qualifier("initSqlsessionFactory")
    @Getter
    private SqlSession session;

    /**
     * 从spring容器里面获得我们的mybatis的mapper
     * @param t1 我们的mapper
     * @param <T>  返回 类型
     * @return
     */
    public static <T> T getMapperInnerContainer(Class<T> t1, ApplicationContext app){
        SqlSessionUtil sqlsessionUtil = app.getBean(SqlSessionUtil.class);
        return (T) sqlsessionUtil.getSession().getMapper(t1);
    }

}
